<html>
<head><meta charset="utf-8"><title>Referencing Locals allocated in registers · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Referencing.20Locals.20allocated.20in.20registers.html">Referencing Locals allocated in registers</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="204802318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Referencing%20Locals%20allocated%20in%20registers/near/204802318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Referencing.20Locals.20allocated.20in.20registers.html#204802318">(Jul 23 2020 at 14:13)</a>:</h4>
<p>Hi, I'm trying to figure out what Rust does when we create a reference to a <code>Local</code> that was allocated in a register. In order to test this I looked at the machine code of some simple programs, but realised that Rust seems to always store <code>Local</code>s on the stack (only moving them into registers temporarily for further computation). For example, compiling <code>let a = 1;</code> I assumed that <code>a</code> would be allocated in a register, but it;s actually allocated on the stack. Wouldn't that be very slow? Maybe I'm just not compiling the right program. I feel like I am missing something here.</p>



<a name="204802402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Referencing%20Locals%20allocated%20in%20registers/near/204802402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Referencing.20Locals.20allocated.20in.20registers.html#204802402">(Jul 23 2020 at 14:13)</a>:</h4>
<p>Enable optimizations, then registers should be used whenever possible</p>



<a name="204802525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Referencing%20Locals%20allocated%20in%20registers/near/204802525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Referencing.20Locals.20allocated.20in.20registers.html#204802525">(Jul 23 2020 at 14:14)</a>:</h4>
<p>Of course, when you take a reference, the variable has to reside on the stack regardless</p>



<a name="204802608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Referencing%20Locals%20allocated%20in%20registers/near/204802608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Referencing.20Locals.20allocated.20in.20registers.html#204802608">(Jul 23 2020 at 14:15)</a>:</h4>
<p>I compiled with <code>--release</code> with the same result.</p>



<a name="204802614"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Referencing%20Locals%20allocated%20in%20registers/near/204802614" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Referencing.20Locals.20allocated.20in.20registers.html#204802614">(Jul 23 2020 at 14:15)</a>:</h4>
<p>Well, if the address is actually necessary, for example something like calling PartialEq::eq which takes references on two ints is unlikely to push things onto the stack in release mode.</p>



<a name="204802640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Referencing%20Locals%20allocated%20in%20registers/near/204802640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Referencing.20Locals.20allocated.20in.20registers.html#204802640">(Jul 23 2020 at 14:15)</a>:</h4>
<p>Do you have the assembly in question?</p>



<a name="204802658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Referencing%20Locals%20allocated%20in%20registers/near/204802658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Referencing.20Locals.20allocated.20in.20registers.html#204802658">(Jul 23 2020 at 14:15)</a>:</h4>
<p>(and program?)</p>



<a name="204802773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Referencing%20Locals%20allocated%20in%20registers/near/204802773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Referencing.20Locals.20allocated.20in.20registers.html#204802773">(Jul 23 2020 at 14:16)</a>:</h4>
<p>Yes, I assume that once I create a reference, the <code>Local</code> is moved to the stack. I assume as long as the reference is alive, I can not move the <code>Local</code> back in to a register?</p>



<a name="204802881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Referencing%20Locals%20allocated%20in%20registers/near/204802881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Referencing.20Locals.20allocated.20in.20registers.html#204802881">(Jul 23 2020 at 14:17)</a>:</h4>
<p>Rust doesn't give you direct knowledge of whether something is on the stack or the register in your program, and the optimization passes are free to do whatever (depending on the types and such in question).</p>



<a name="204802897"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Referencing%20Locals%20allocated%20in%20registers/near/204802897" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Referencing.20Locals.20allocated.20in.20registers.html#204802897">(Jul 23 2020 at 14:17)</a>:</h4>
<p>The program is (no references in this example as I was trying to get Rust to use a register first)</p>
<div class="codehilite"><pre><span></span><code>fn main() {
    let mut a = 0;
    a = 3;
    println!(&quot;{}&quot;, a);
}
</code></pre></div>



<a name="204803004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Referencing%20Locals%20allocated%20in%20registers/near/204803004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Referencing.20Locals.20allocated.20in.20registers.html#204803004">(Jul 23 2020 at 14:18)</a>:</h4>
<p><code>println</code> takes a reference</p>



<a name="204803067"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Referencing%20Locals%20allocated%20in%20registers/near/204803067" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Referencing.20Locals.20allocated.20in.20registers.html#204803067">(Jul 23 2020 at 14:18)</a>:</h4>
<p>and the formatting infra is specifically tailored to not get inlined etc, optimizing for code size rather than speed for the most part</p>



<a name="204803141"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Referencing%20Locals%20allocated%20in%20registers/near/204803141" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Referencing.20Locals.20allocated.20in.20registers.html#204803141">(Jul 23 2020 at 14:19)</a>:</h4>
<p>Ah, so because I'm printing <code>a</code> the compiler knows it needs to reference it, so it never gets allocated in a register in the first place.</p>



<a name="204803599"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Referencing%20Locals%20allocated%20in%20registers/near/204803599" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Referencing.20Locals.20allocated.20in.20registers.html#204803599">(Jul 23 2020 at 14:22)</a>:</h4>
<p>Okay, this maybe more of a LLVM question then, but let's say <code>a</code> gets allocated in a register. I then reference <code>a</code> somewhere, which means it needs to be moved to the stack. How does the compiler know when it can move <code>a</code> back into a register. I assume once all references to <code>a</code> are dead?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>